package com.xyulu.controller.blog;

import com.xyulu.entity.BlogArticleEntity;
import com.xyulu.entity.BlogTagEntity;
import com.xyulu.entity.common.PageBean;
import com.xyulu.service.BlogArticleService;
import com.xyulu.service.BlogTagService;
import com.xyulu.utils.ShiroUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description TODO
 * @Author luming
 * @Date 2021/3/24
 * @Version 1.0
 */
@Controller
public class IndexController {

    @Resource
    private BlogArticleService articleService;
    @Resource
    private BlogTagService tagService;


    @GetMapping("/")
    public String index(){
        return "redirect:index/1";
    }

    @GetMapping("/index/{id}")
    public String index(HttpServletRequest request, @PathVariable("id") Integer page){

        PageBean pageBean = new PageBean(page,"/index","");
        List<BlogArticleEntity> articles = articleService.findArticleViews(pageBean);
        List<BlogTagEntity> tags = tagService.list();
        List<BlogArticleEntity> topViews =  articleService.topViews();

        request.setAttribute("pageBean",pageBean);
        request.setAttribute("articles",articles);
        request.setAttribute("tags",tags);
        request.setAttribute("topViews",topViews);
        request.setAttribute("user",ShiroUtil.getLocalUserName());

        return "blog/index";
    }

    @GetMapping("/login")
    public String toLogin(){
        return "blog/login";
    }

}
